﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Acr.XamForms.UserDialogs;
using LiveMakkah.Models;
using LiveMakkah.Services;
using LiveMakkah.Utils;
using Xamarin.Forms;

namespace LiveMakkah.ViewModels
{
    public class ChannelListViewModel : BaseViewModel
    {
        private readonly IChannelListService _channelList;
        private List<FarahnakModel> _channels;
        private FarahnakModel _selectedchannel;

        public ChannelListViewModel()
        {
            _channelList = DependencyService.Get<IChannelListService>(); 
        }

        public override void OnAppearing()
        {
            base.OnAppearing();
            GetChannelsAsyncTask();
        }


        public List<FarahnakModel> Channels
        {
            get { return _channels; }
            set
            {
                _channels = value;
                _channels=_channels.Where(x => x.Type!=1).ToList();
                OnPropertyChanged();
            }
        }

        public async void GetChannelsAsyncTask()
        {

            Dialogs.ShowLoading();
            try
            {
                Channels = await _channelList.GetChannels();
            }
            catch (Exception e)
            {
                Dialogs.HideLoading();
                var ac = new AlertConfig {Message = e.Message};
                Dialogs.Alert(ac);
            }
            finally
            {
                Dialogs.HideLoading();
            }
           
        }

        public FarahnakModel SelectedChannel
        {
            get { return _selectedchannel; }
            set
            {
                if (_selectedchannel == value)
                    return;
                _selectedchannel = value;
                OnPropertyChanged();
                if (_selectedchannel != null)
                {
                    var todovm = new ChannelViewModel(_selectedchannel);

                    App.MainNavigationPage.PushAsync(ViewFactory.CreatePage(todovm));
                    _selectedchannel = null;
                }
            }
        }
    }
}
